package com.itheima.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/**
 * @author chenxys
 * @Description: 七牛云文件上传工具类
 * @date 2022/5/22 11:22
 */
public class QiNiuUtils {
    /**
     * ACCESS_KEY
     */
    private static final String ACCESS_KEY = "0FtNhSaxD9qGFJeu50M_4Dt-__HthT9SG4jMZRfL";

    /**
     * SECRET_KEY
     */
    private static final String SECRET_KEY = "X-FjFLowfkZT52Nu5Xk0FjOpVKu0w8syOC_LtxWF";

    /**
     * BUCKET
     * 七牛云仓库名称
     */
    private static final String BUCKET = "darkhealth";

    /**
     * 上传文件
     * @author chenxys
     * @param filePath
     * @param fileName
     * @return void
     */
    public static void upload1QiNiu(String filePath,String fileName){
        //构造一个带指定 Region 对象的配置类     存储空间对应的区域
        Configuration cfg = new Configuration(Region.huanan());
        UploadManager uploadManager = new UploadManager(cfg);
        //根据ak，sk创建一个授权对象
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(BUCKET);
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println("文件名称："+putRet.key);
            System.out.println("文件hash值："+putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    /**
     * 字节流文件上传
     * @author chenxys
     * @param bytes
     * @param fileName
     * @return void
     */
    public static void upload2QiNiu(byte[] bytes,String fileName){
        //构造一个带指定 Region 对象的配置类     存储空间对应的区域
        Configuration cfg = new Configuration(Region.huanan());
        UploadManager uploadManager = new UploadManager(cfg);
        //根据ak，sk创建一个授权对象
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(BUCKET);
        try {
            Response response = uploadManager.put(bytes, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println("文件名称："+putRet.key);
            System.out.println("文件hash值："+putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    /**
     * 删除文件
     * @author chenxys
     * @param fileName
     * @return void
     */
    public static void deleteQiNiu(String fileName){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(BUCKET, fileName);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}
